package com.yanxu.threadexception;

/**
 * @author 折戟沉沙铁未销
 * @version V1.0
 * @date 2025/1/18-2025
 * @Description: 不能捕获异常
 */
public class CantCatchException2 implements Runnable{
    public static void main(String[] args) throws InterruptedException {
        try {
            for (int i = 0; i < 100; i++) {
                System.out.println("main "+i);
            }

            new Thread(new CantCatchException2(),"thread1").start();
            new Thread(new CantCatchException2(),"thread2").start();
            new Thread(new CantCatchException2(),"thread3").start();
            new Thread(new CantCatchException2(),"thread4").start();

            Thread.sleep(100);
            for (int i = 100; i < 200; i++) {
                System.out.println("main "+i);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        /*
        * 使用传统的 try-catch 在主线程中进行捕获，是无法捕获子线程中的异常的。
        *
        * try-catch 只能捕获自己线程内的异常。
        * */

    }

    public void run() {
        throw new RuntimeException(Thread.currentThread().getName() + " :抛出了 runtime exception 异常了!!!");
    }
}